﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules.View;
using Basic4Net.Controls;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;
using Basic4Net.BusinessRules;

namespace Basic4Net.OAWeb.Module_Workload
{
    public partial class FixedWorkInput : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDropList();
                //  InitTeachData();
                Bind(0, aspNetPager.PageSize);
            }
        }


        /// <summary>
        /// 初始化职称下拉框
        /// </summary>
        private void InitDropList()
        {
            //List<t_Post> list = Post.PostInstance.FindAll(SchoolMark);

            ////添加一个“其他”选项
            ////t_Post item = new t_Post();
            ////item.PostName = "无职称";
            ////item.RID = -1;
            ////list.Add(item);
            //dropList1.DataSource = list;
            //dropList1.DataValueField = "RID";
            //dropList1.DataTextField = "PostName";


            this.dropList1.DataValueField = "RID";
            this.dropList1.DataTextField = "TypeName";
            this.dropList1.DataSource = (new BusinessRules.TeacherType()).ListAll();   
            dropList1.DataBind();
            //ListItem item2 = new ListItem();
            //item2.Value = "-1";
            //item2.Text = "----全部----";
            //this.dropList1.Items.Add(item2);
            //this.dropList1.SelectedValue = "-1";
        }

        /// <summary>
        /// 分页控件事件
        /// </summary>
        protected void AspNetPagerPageChanged(object sender, BNPager.PageChangedEventArgs e)
        {
            aspNetPager.PageIndex = e.NewPageIndex;
            Bind((e.NewPageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }


        private void Bind(int startAt, int step)
        {
            //string teachName = TeachName.Text;
            int year = CurrentYear;
            int month = CurrentMonth;
            string teachCode = TeachCode.Text;
            int postId = dropList1.SelectedItem.Value == null ? -1 : Convert.ToInt32(dropList1.SelectedItem.Value);
            int count = 0;
            IList<TeachLessonList> teacherList = null;
            if (postId > 0) 
                teacherList = Lesson.LessonInstance.FindLessonByTeachPost(startAt, step, teachCode,
                                                                            SchoolMark,postId, year,
                                                                            month, ref count); 
            aspNetPager.RecordCount = count;
            Repeater1.DataSource = teacherList;
            Repeater1.DataBind(); 
        }


        /// <summary>
        /// 职务分类下拉事件
        /// </summary>
        protected void DrPostChanged(object sender, EventArgs e)
        {
            Bind(0, aspNetPager.PageSize);
        }



        protected void btnItemBind_Click(object sender, EventArgs e)
        {
            Bind((aspNetPager.PageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }


        /// <summary>
        /// 设置教师课时
        /// </summary>
        /// <param name="arrTeacher"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod]
        public static int UpdateLessonNum(int lessonId, double lessonNum)
        {
            if (lessonId <= 0 )
                return -1;
            bool b = Lesson.LessonInstance.UpdateTeachLesson(lessonId, lessonNum);
            if (b)
                return 1;
            else
                return -1;
        }


        [System.Web.Services.WebMethod]
        public static int AddLessonNum(int teacherId, double lessonNum)
        {
            if (teacherId <= 0 || lessonNum <= 0)
                return -1;

            t_Lesson entity = new t_Lesson();
            entity.SchoolMark = SchoolMark;
            entity.FYear = CurrentYear;
            entity.FMonth = CurrentMonth;
            entity.TeacherID = teacherId;
            entity.Lesson = lessonNum;

            bool b = Lesson.LessonInstance.AddTeachLesson(entity);
            if (b)
                return 1;
            else  
                return -1; 
        }
    }
}